#!/bin/tcsh -f

#TODO: add template creation

set DATADIR = /data1

set QCdir = $cwd/pics

set regTemplate = $cwd/atlas/template_surfSPHARM_ellalign.meta
set fliptemplate = $cwd/atlas/template_surfSPHARM_ellalign.coef

set subdiv = 10
set degree = 12

set spacing = "0.7,0.7,0.7"

# visualization setting for ellips alligned meshes (adapt these to your selected viewpoint)
set phiPar = "--posx 0 --posy 0 --posz 12 --focx 0 --focy 0 --focz 0 --upx 0 --upy 1 --upz 0 --zoom 3 --distMapMin 0 --distMapMax 6.28 --br 255 --bg 255 --bb 255"
set thetaPar = "--posx 0 --posy -10 --posz -2 --focx 0 --focy 0 --focz 0 --upx -1 --upy 0 --upz 0 --zoom 3 --distMapMin 0 --distMapMax 3.14 --br 255 --bg 255 --bb 255"
set phiFile = $cwd/atlas/template_surf_paraPhiHalf.txt
set phiFile = $cwd/atlas/template_surf_paraPhi.txt
set thetaFile = $cwd/atlas/template_surf_paraTheta.txt

foreach Case ($DATADIR/??_labels.gipl)

  SegPostProcess $Case -o $Case:r_pp.gipl.gz -label 1 -space $spacing

end

echo SegPostProcess Done

# Now generate the parametrization and compute the SPHARM coefs and associated PDM's

foreach Case ($DATADIR/*_pp.gipl)
   GenParaMesh $Case -iter 1000 -label 1
   ParaToSPHARMMesh $Case:r_surf.meta $Case:r_para.meta -subdivLevel $subdiv -spharmDegree $degree -flipTemplate $fliptemplate -regTemplate $regTemplate
   endif

end

echo SPHARM Done

# Now generate the QC images
foreach case ($DATADIR/*procalign.meta)
    KWMeshVisu -mesh $case -image $case:r_phi.bmp -scalar $phiFile $phiPar
    KWMeshVisu -mesh $case -image $case:r_theta.bmp -scalar $thetaFile $thetaPar
end

#collect the images
if (! -e $QCdir) mkdir $QCdir

set tile = "6x5"
set num = 30
set size = 150

montage +frame +shadow +label -geometry $size -tile $tile $DATADIR/*_phi.bmp $QCdir/QC_Phi.jpg
montage +frame +shadow +label -geometry $size -tile $tile $DATADIR/*_theta.bmp $QCdir/QC_Theta.jpg

echo QC Done
